package com.onluy.sysman.controller;

import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.base.AppController;
import com.onluy.frameworkcloud.base.AppService;
import com.onluy.frameworkcloud.batis.dbtobean.CreateBeanConfig;
import com.onluy.frameworkcloud.batis.dbtobean.CreateBeans;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.MakeControllerParam;
import com.onluy.sysman.param.MakeEntityParam;
import com.onluy.sysman.param.UserParam;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.util.List;



@Validated
@RestController
@RequestMapping("/automation")
@Api(value="automation", description="/代码自动化", position=9999 )
public class AutomationController extends AppController {


	@Override
	public AppService service() {
		return null;
	}





	@RequestMapping(value={"/makeEntity"},method={ RequestMethod.POST})
	@ApiOperation(value="生成实体", notes="自动生成实体类")
	public String mkenty( @Valid @RequestBody ReqInfo<ReqHead, MakeEntityParam> reqInfo ) throws Exception {
		MakeEntityParam  makeEntityParam=reqInfo.getReqBody();
		String msg="生成成功";
		if(   makeEntityParam==null)throw new Exception("makeEntityParam 不能为空！");

		try {
			CreateBeanConfig appConfig = new CreateBeanConfig();
			appConfig.setDriverClassName(makeEntityParam.getDriver());
			appConfig.setDburl(makeEntityParam.getUrl());
			appConfig.setDbusername(makeEntityParam.getUsername());
			appConfig.setDbpassword(makeEntityParam.getPassWord());
			String[][] tables = new String[][] { new String[] { makeEntityParam.getTableName(), makeEntityParam.getPackageName(), makeEntityParam.getPackagPath() } };
			CreateBeans.createJavaBean(tables, appConfig);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			msg="生成异常："+e.getMessage();
		}
		return msg;
	}


	@RequestMapping(value={"/makeController"},method={ RequestMethod.POST})
	@ApiOperation(value="生成实体", notes="自动生成实体类")
	public String makeController( @Valid @RequestBody MakeControllerParam makeControllerParam ) throws Exception {
		String msg = "生成成功";

		String controllername=makeControllerParam.getControllername();
		String packageName=makeControllerParam.getPackageName();
		String packagPath=makeControllerParam.getPackagPath();


		File rfile = ResourceUtils.getFile("classpath:automationmoban/controller.moban");
		File wfile =  new File(packagPath+controllername+"Controller.java");


		if(wfile.exists())throw  new Exception("Controller 已经存在，无法再次生成");

		FileInputStream fis = new FileInputStream(rfile);
		InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
		BufferedReader bReader = new BufferedReader(isr);

		StringBuilder sb = new StringBuilder();//定义一个字符串缓存，将字符串存放缓存中
		String s = "";
		while ((s = bReader.readLine()) != null) {//逐行读取文件内容，不读取换行符和末尾的空格
			sb.append(s + "\n");//将读取的字符串添加换行符后累加存放在缓存中
		}
		bReader.close();
		isr.close();
		fis.close();
		String tempcontrollername = controllername.substring(0, 1).toLowerCase() + controllername.substring(1);
		msg = sb.toString().replaceAll("TfMgrDepart", controllername)
				.replaceAll("tfMgrDepart", tempcontrollername)
				.replaceAll("===package===", packageName) ;



		FileOutputStream fos = new FileOutputStream(wfile);
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		osw.write(msg);
		osw.flush();
		osw.close();
		fos.close();

		return msg;
	}

}
